package com.lik888.web1.controller;

//@PreAuthorize("authentication.principal.id == T(com.lik888.util.ThreadLocalUtil).getOrderId()")
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.lik888.web1.entity.DTO.UserTableDTO;
import com.lik888.web1.entity.R;
import com.lik888.web1.entity.UserTable;
import com.lik888.web1.service.IUserTableService;
import com.lik888.web1.service.impl.UserTableServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-10-30
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserTableController {
    private final IUserTableService userTableService;

    @PostMapping("/login")
    public R login(@RequestParam String username, @RequestParam String password) {
        String login = userTableService.Login(username, password);
        return R.ok(login);
    }

    @PostMapping("/register")
    public R register(UserTable userTable) {
        Boolean register = userTableService.Register(userTable);
        return R.ok(register);
    }


    @GetMapping("/check")
    @PreAuthorize("hasAuthority('default')")
    public Boolean check() {
        return userTableService.CheckPhone();
    }

    //二次确认是否绑定手机号和邮箱
    @PostMapping("/doubleregister")
    @PreAuthorize("hasAuthority('default')")
    public R doubleregister(@RequestParam("phone") String phone, @RequestParam("email") String email) {
        R r = userTableService.DoubleRegister(phone, email);
        return r;
    }

    //获取用户个人信息
    @GetMapping("/usermessage")
    @PreAuthorize("hasAuthority('default')")
    public R info() {
        R r = userTableService.GetUserMessage();
        return r;
    }

    //忘记密码功能  1.确认用户名和邮箱是否绑定 确认用户名是否存在 确认邮箱是否存在  2.邮箱发送验证码 3.用户输入验证码确认 4.重置密码 4个接口实现
    @PostMapping("/forgetemail")
    public R forgetpassword(@RequestParam("email") String email, @RequestParam("username") String username) {
        R r = userTableService.ForgetCheck(email, username);
        return r;
    }


    //验证码确认！
    @PostMapping("/returncode")
    public R returncode(@RequestParam("username") String username,@RequestParam("code") String code) {
        Boolean aBoolean = userTableService.ReturnCode(username, code);
        return R.ok(aBoolean);
    }

    //密码重置！
    @PostMapping("/resetpassword")
    public R resetpassword(@RequestParam("username") String username,@RequestParam("password") String password,@RequestParam("isTrue") Boolean isTrue) {
        R<UserTable> userTableR = userTableService.ResetPassword(username, password,isTrue);
        return R.ok(userTableR);
    }


    //个人信息修改功能接口
    @PostMapping("/changeself")
    @PreAuthorize("hasAuthority('default')")
    public R changeself(@RequestBody UserTableDTO userTableDTO) {
        UserTable userTable = userTableService.ChangeSelf(userTableDTO);
        return R.ok(userTable);
    }

    //身份证号确认接口
    @GetMapping("/checkidcard")
    @PreAuthorize("hasAuthority('default') && @check.isSelf(authentication)")
    public R checkiacard() {

        Boolean checkId = userTableService.CheckIdCard();
        return R.ok(checkId);
    }

    //购买 或是 出售时 要求填写身份证号
    @PostMapping("/checkidcardforbuyorsell")
    @PreAuthorize("hasAuthority('default') && @check.isSelf(authentication)")
    public R checkidcardforbuyorsell(@RequestParam("idcard") String idcard, @RequestParam("boolean") Boolean isTrue) {
        UserTable userTable = userTableService.CheckIdCardForBuyOrSell(idcard, isTrue);
        return R.ok(userTable);
    }
}
